package com.ikun.admin.modules.sys.controller;

import com.ikun.admin.common.annotation.OperationAnno;
import com.ikun.admin.common.aspect.OperationAspect;
import com.ikun.admin.modules.gen.mapper.TableEntityMapper;
import com.ikun.admin.modules.sys.entity.Role;
import com.ikun.admin.modules.sys.entity.RoleMenu;
import com.ikun.admin.modules.sys.entity.User;
import com.ikun.admin.modules.sys.iservice.IAclService;
import com.ikun.admin.modules.sys.vo.*;
import com.ikun.common.result.Result;
import com.ikun.common.search.PageData;
import com.ikun.common.search.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName AclController
 * @Description:
 * @CreateDate: 2024/5/31 23:43
 */

@Api("权限控制相关接口")
@RestController
@RequestMapping("/sys/acl")
public class AclController {

    @Autowired
    private IAclService iAclService;
    @Autowired
    private OperationAspect operationAspect;
    @Autowired
    private TableEntityMapper tableEntityMapper;

    @PostMapping("/menu/select/API_001")
    @ApiOperation("权限菜单接口")
    public Result<PermMenuVo> selectPermMenu() {
        return iAclService.selectMenuPerm();
    }

    @PostMapping("/user/list/API_002")
    @ApiOperation("用户分页查询vo")
    public Result<PageResult<UserListResultVo>> userList(@RequestBody PageData<UserListParamVo> pageData) {
        return iAclService.userList(pageData);
    }

    @GetMapping("/user/select/API_003/{id}")
    @ApiOperation("用户详情接口")
    public Result<UserDetailVo> userDetail(@PathVariable Long id) {
        return iAclService.userDetail(id);
    }

    @GetMapping("/role/dict/API_004")
    @ApiOperation("角色字典接口")
    public Result<List<RoleDictVo>> roleDict() {
        return iAclService.roleDict();
    }

    @PostMapping("/user/addOrUpdate/API_005")
    @ApiOperation("用户新增或者编辑接口")
    @OperationAnno(type = 0, tableEntity = User.class)
    public Result<String> userAddOrUpdate(@RequestBody UserAddOrUpdateVo userAddOrUpdateVo) {
        return iAclService.addOrUpdateUser(userAddOrUpdateVo);
    }

    @DeleteMapping("/user/delete/API_006/{id}")
    @ApiOperation("用户删除接口")
    @OperationAnno(type = 1, tableEntity = User.class)
    public Result<String> deleteUser(@PathVariable Long id) {
        return iAclService.deleteUser(id);
    }

    @PostMapping("/role/list/API_007")
    @ApiOperation("角色查询接口")
    public Result<PageResult<RoleDictVo>> roleList(@RequestBody PageData<RoleListParamVo> pageData) {
        return iAclService.roleList(pageData);
    }

    @GetMapping("/role/select/API_008/{id}")
    @ApiOperation("角色详情接口")
    public Result<RoleDictVo> roleDetail(@PathVariable Long id) {
        return iAclService.roelDetail(id);
    }

    @PostMapping("/role/addOrUpdate/API_009")
    @ApiOperation("新增或修改角色接口")
    @OperationAnno(type = 0, tableEntity = Role.class)
    public Result<String> addOrUpdateRole(@RequestBody RoleDictVo roleDictVo) {
        return iAclService.addOrUpdateRole(roleDictVo);
    }

    @DeleteMapping("/role/detete/API_010/{id}")
    @ApiOperation("删除角色接口")
    @OperationAnno(type = 1, tableEntity = Role.class)
    public Result<String> deleteRole(@PathVariable Long id) {
        return iAclService.deleteRole(id);
    }

    @GetMapping("/perm/tree/API_011")
    @ApiOperation("获取所有的树形结构权限")
    public Result<List<PermTreeNodeVo>> permTree() {
        return iAclService.permTree();
    }

    @GetMapping("/roleMenu/select/API_012/{id}")
    @ApiOperation("根据角色id查询当前角色拥有的权限")
    public Result<List<Long>> selectMenuRole(@PathVariable Long id) {
        return iAclService.selectMenuByRoleId(id);
    }

    @PostMapping("/roleMenu/addOrUpdate/API_013")
    @ApiOperation("编辑角色权限")
    @OperationAnno(type = 0, tableEntity = RoleMenu.class)
    public Result<String> roleMenuAddOrUpdate(@RequestBody RoleMenuAddOrUpdateVo roleMenuAddOrUpdateVo) {
        return iAclService.addOrUpdateRoleMenu(roleMenuAddOrUpdateVo);
    }

    @GetMapping("/user/dict/API_014")
    @ApiOperation("获取用户字典,排除当前用户和超级管理员")
    public Result<List<UserDictVo>> selectUserDict() {
        return iAclService.selectUserDict();
    }

    @PostMapping("/user/export/API_015")
    @ApiOperation("导出用户")
    public void exportUser(HttpServletResponse response, @RequestBody PageData<UserListParamVo> pageData) {
        iAclService.exportUser(response,pageData);
    }

    @PostMapping("/user/import/API_016")
    @ApiOperation("导入用户")
    public Result<String> importUser(@RequestBody MultipartFile file) {
        return iAclService.importUser(file);
    }
}
